package cn.com.dcsgo.service.impl;

import cn.com.dcsgo.mapper.UserMapper;
import cn.com.dcsgo.pojo.User;
import cn.com.dcsgo.service.UserService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

/**
 * UserServiceImpl
 *
 * @author Dcsgo
 * @since 2025/2/25 14:07
 */
@Service
public class UserServiceImpl implements UserService {
    private final UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper) {
        this.userMapper = userMapper;
    }

    @Override
    public User login(String username, String password) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", username);
        queryWrapper.eq("password", password);
        return userMapper.selectOne(queryWrapper);
    }

    @Override
    public boolean addUser(User user) {
        if (user == null) {
            return false;
        }
        return userMapper.insert(user) > 0;
    }

    @Override
    public boolean removeById(Long id) {
        if (id == null) {
            return false;
        }
        return userMapper.deleteById(id) > 0;
    }

    @Override
    public User updateUser(User user) {
        if (user == null || user.getId() == null) {
            return null;
        }
        if (userMapper.updateById(user) <= 0) {
            return null;
        }

        return userMapper.selectById(user.getId());
    }

    @Override
    public User queryById(Long id) {
        return userMapper.selectById(id);
    }
}
